<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>创建调查预览页</title>
    <link rel="stylesheet" type="text/css" href="../../css/mui.min.css">
    <link rel="stylesheet" type="text/css" href="../../css/base.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/study.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
    <style type="text/css" media="screen">
        .next_btn {
            width: 92%;
            height: 67%;
            font-size: 16px;
            background: url(../../icon/add_research.png) no-repeat 36% 50%;
            background-size: 20px 20px;
            background-color: #18b4ed;
        }

        .mui-btn-warning, .mui-btn-yellow {
            color: #fff;
            border: 1px solid #18b4ed;
        }

        .mui-btn-warning:enabled:active {
            color: #fff;
            border: 1px solid #18b4ed;
            background-color: #18b4ed;
        }

        .nav-item:active {
            color: #fff;
        }
    </style>
</head>
<body class="">
<div class="base_style_color " id='BarAppearance'></div>
<div class="base_navigation_bar base_style_color base_navigation_item_bar">
    <a class="base_left_item  nav-item" href="#" onclick="backToast()" style="line-height:44px;color: #fff;"> 取消 </a>
    <span id="newsTitle">创建调查</span>
    <a class="base_right_item nav-item" style="line-height: 44px" onclick="submit()"> 完成 </a>
</div>
<div id="offlineAlert" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd">
            <strong class="weui_dialog_title" id="checkNum">是否放弃当前页面的内容创建？</strong>
        </div>
        <div class="weui_dialog_bd"></div>
        <div class="weui_dialog_ft">
            <a onclick="cleanCancel()" href="#" class="weui_btn_dialog default" style="color:#18b4ed">取消</a>
            <a onclick="cleanConfirm()" href="#" class="weui_btn_dialog primary" style="color:#18b4ed">确定</a>
        </div>
    </div>
</div>
<div id="showAlert" class="weui_dialog_confirm" style="display: none;">
    <div class="weui_mask"></div>
    <div class="weui_dialog">
        <div class="weui_dialog_hd">
            <strong class="weui_dialog_title">是否确定开启此调查？</strong>
        </div>
        <div class="weui_dialog_bd"></div>
        <div class="weui_dialog_ft">
            <a href="#" class="weui_btn_dialog default" id="openSign_no" style="color:#18b4ed">取消</a>
            <a href="#" class="weui_btn_dialog primary" id="openSign_yes" style="color:#18b4ed">确定</a>
        </div>
    </div>
</div>
<div id="bottom_bar" class="course_select_footer " style="padding-top: 6px;padding-bottom: 6px">
    <button id="sumbit_bt" type="button" class="mui-btn-warning horcenter next_btn " onclick="addQuestion()">
        添加问题
    </button>
</div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script src="../../script/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="../../script/MD5.js"></script>
<script src="../../script/common.js"></script>
<script type="text/javascript" src="../../script/requestTool.js"></script>
<script type="text/javascript" src="../../script/ui.js"></script>
<script type="text/javascript">
    var questionArr2 = new Array();
    var questionArr = new Array();
    var questionArr3 = '';
    var className = '';
    var classid = '';
    var name = '';
    var classarr = [];
    var daan = new Object();
    var ResearchName = '';
    var Research_className = '';
    var Research_classid = '';
    var Research_submit_toast = '';
    var iid = "";
    apiready = function () {
    	initHeaderUI();
        ReceNotifi();
        if (api.pageParam.type1 == 4) {//编辑题目时
            setupSubframe2(api.pageParam.type1);
        } else if (api.pageParam.type1 = 8 || api.pageParam.type1 == 9) {
            daan.questionitems = api.pageParam.data;
            classarr = api.pageParam.classarr;
            setupSubframe2(api.pageParam.type1);
        }
        $('#title').text('创建' + ludongJudge());
        btnClick();
    };

    function sendBuriedNotifi(busionessid, pagetype, pagetypeid) {
        var userinfo = {
            uid: $api.getStorage('cfnetppuid'),
            jid: $api.getStorage('cfnetppjxid'),
            uname: $api.getStorage('cfnetppusername'),
        }
        var data = {
            busionessid: busionessid,
            pagetype: pagetype,
            pagetypeid: pagetypeid,
            sourceurl: api.pageParam.sourceurl,
            sourcetitle: api.pageParam.sourcetitle,
            accesspageurl: window.location.pathname,
            accesspagetitle: document.title,
            userinfo: userinfo
        }
        SendBehaviorTrajectoriesNotifi_Study(data);
    }

    //按钮点击事件
    function btnClick() {
        $('#openSign_no').click(function () {
            $('#showAlert').hide();
            returnList()
        });
        //正常情况下是否退出当前考试按钮确定键
        $('#openSign_yes').click(function () {
            $('#showAlert').hide();
            start(iid)
        });
    }

    //是否是鲁东智慧课堂
    function ludongJudge() {
        //读取消息推送
        var jid = $api.getStorage('cfnetppjxid');
        if (jid != 425) {//非鲁东智能课堂
            return '调查'
        } else {
            return '投票'
        }
    }

    //退出提示
    function backToast() {
        if ($api.getStorage('ResearchName') == '' && $api.getStorage('Research_className') == '' && ($api.getStorage('Research_submit_toast') == '' || $api.getStorage('Research_submit_toast') == undefined)) {
            returnList();
        } else {
            $('#offlineAlert').show();
            api.setFrameAttr({
                name: 'Teacher_AddResearchDetail',
                hidden: true
            });
        }
    }

    //确定
    function cleanConfirm() {
        $('#offlineAlert').hide();
        returnList();
    }

    //取消
    function cleanCancel() {
        $('#offlineAlert').hide();
        api.setFrameAttr({
            name: 'Teacher_AddResearchDetail',
            hidden: false
        });
    }

    function backClick() {
        api.closeWin();
    }

    //接收通知
    function ReceNotifi() {
        api.addEventListener({
            name: 'deleteQuestionNotifi'
        }, function (ret, err) {
            questionArr2 = ret.value.data;
            daan.questionitems = ret.value.data;
        });
        //获取班级数组
        api.addEventListener({
            name: 'refreshClassData'
        }, function (ret, err) {
            //coding...
            if (ret.value) {
                classarr = ret.value.data;
            }
        });
        api.addEventListener({
            name: 'keyback'
        }, function (ret, err) {
            //coding...
            backToast();
        });
    }

    //编辑时,打开列表
    function setupSubframe2(type1) {
        var pageParam = '';
        if (type1 = 8 || type1 == 9) {
            pageParam = {
                id: $api.getStorage('UpdateId'),
                name: $api.getStorage('ResearchName'),
                classname: $api.getStorage('Research_className'),
                type1: api.pageParam.type1,
                data: api.pageParam.data,
                classarr: api.pageParam.classarr,
                pid: api.pageParam.pid,
                cid: api.pageParam.cid,
            };
        } else {
            pageParam = {};
        }
        var y = $('.base_navigation_bar').height() + 22;
        var h = api.winHeight - y - 54;
        api.pageParam['sourceurl'] = api.pageParam.sourceurl;
        api.pageParam['sourcetitle'] = api.pageParam.sourcetitle;
        api.openFrame({
            name: 'Teacher_AddResearchDetail',
            url: 'Teacher_AddResearchDetail.html',
            rect: {
                x: 0,
                y: y,
                w: 'auto',
                h: h
            },
            pageParam: pageParam,
            reload: true
        });
    }

    //添加问题
    function addQuestion() {
        ResearchName = $api.getStorage('ResearchName');
        Research_className = $api.getStorage('Research_className');
        if (ResearchName == '') {
            ShowToast('请填写' + ludongJudge() + '名称');
            return false
        }
        api.openWin({useWKWebView:true,
            name: 'Teacher_AddResearchQuestion',
            url: 'Teacher_AddResearchQuestion.html',
            pageParam: {
                data: questionArr2,
                type: 1,
                classarr: classarr,
                pid: api.pageParam.pid,
                cid: api.pageParam.cid,
                sourceurl: api.pageParam.sourceurl,
                sourcetitle: api.pageParam.sourcetitle
            },
            slidBackEnabled: false,
            reload: true
        });
    }

    //完成按钮点击触发事件
    function submit() {
        ResearchName = $api.getStorage('ResearchName');
        Research_className = $api.getStorage('Research_className');
        Research_classid = $api.getStorage('Research_classid');
        Research_submit_toast = $api.getStorage('Research_submit_toast');
        if (ResearchName == '' || isOnlySpaceOrEnter(ResearchName)) {
            ShowToast('请填写' + ludongJudge() + '名称');
            return false
        }
        if (daan.questionitems == '' || daan.questionitems == undefined) {
            ShowToast('请添加问题');
            return false
        }
        submitData();
    }

    //提交数据
    function submitData() {
        if ($api.getStorage('Research_submit_toast') == '') {
            Research_submit_toast = '谢谢参与！'
        }
        var param = {
            jid: $api.getStorage('cfnetppjxid'),
            uid: $api.getStorage('cfnetppuid'),
            tid: 3,
            classid: '',
            name: ResearchName,
            content: Research_submit_toast,
            question: daan,
            pid: api.pageParam.pid,
            cid: api.pageParam.cid,
        };
        cfnetppPOST(url_addActive, param, true, function (data, status) {
            SendBusinessOperationsLogNotifi('老师端调查创建', '199', param, param, '', data, '老师端调查创建接口', '199-01')
            hideProgress();
            if (status == 'success') {
                if (data.code == 200) {
                    ShowToast('提交成功');
                    iid = data.iid;
                    $('#showAlert').show();
                    api.setFrameAttr({
                        name: 'Teacher_AddResearchDetail',
                        hidden: true
                    });
                    sendNotifi();
                    //						returnList();
                } else if (data.code == 110) {
                    ShowToast(ludongJudge() + '名称已存在');
                } else {
                    ShowToast(data.message);
                }
            } else {
                api.toast({
                    msg: '网络请求失败',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        });
    }

    //开始活动
    function start(iid) {
        var params = {
            tid: 3,
            iid: iid,
            uid: $api.getStorage('cfnetppuid'),
            jid: $api.getStorage('cfnetppjxid'),
            action: "start",
        }
        cfnetppPOST(url_actionDosign, params, false, function (data, status) {
            SendBusinessOperationsLogNotifi('老师端调查开始', '197', params, params, '', data, '老师端调查开始接口', '197-01')

            api.hideProgress();
            if (status == 'success') {
                if (data.code == 200) {
                    openResearch(iid)
                } else {
                    ShowToast('开启失败');
                    api.setFrameAttr({
                        name: 'Teacher_AddResearchDetail',
                        hidden: false
                    });
                }
            } else {
                ShowToast('开启失败');
                api.setFrameAttr({
                    name: 'Teacher_AddResearchDetail',
                    hidden: false
                });
            }
        })
    }

    //打开调查详情页
    function openResearch(id) {
        api.openWin({useWKWebView:true,
            name: 'Teacher_ResearchListDetail',
            url: 'Teacher_ResearchListDetail.html',
            pageParam: {
                id: id,
                name: $api.getStorage('ResearchName'),
                pid: api.pageParam.pid,
                cid: api.pageParam.cid,
                sourceurl: api.pageParam.sourceurl,
                sourcetitle: api.pageParam.sourcetitle
            },
        });
    }

    //发送通知
    function sendNotifi() {
        api.sendEvent({
            name: 'RerfeshThList'
        });
    }

    //返回列表页
    function returnList() {
        sendBuriedNotifi('1', '老师端调查创建页', '199');
        api.closeToWin({
            name: 'Teacher_ResearchWindow'
        });
    }
</script>
</html>
